<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/6/25
 * Time: 11:27
 */
namespace Home\Aip;
use Lib\PasswordHash;
use Think\Controller;

class Store extends Controller{
    protected $st_store = 'store';
    public $actioninfo = '';//返回操作字符

    /**
     * 数据检查
     */
    function _emptyChecked($data,$msg){
		$i = 0;
		foreach ($data as $key=>$v) {
			//判断$data是否是一维数组
			$mach = preg_match('/^\d+$/i', $key);
			if (!$mach) {
				$i = $i + 1;
				$type = 1;
			} else {
			    $i = $key ;
				$type = 2;
			}	
			
			if ($v=='' && $msg[$i-1]) {				
				return $msg[$i-1];
			}			
		}
		return $type ;	
    }	
    /**
     * 数据添加
     */
    function _dataAdd($table,$data,$msg,$name){
		
		//$type = $this->_emptyChecked($data,$msg);
		//一维数组
		//if ($type == '1') {
			
			//添加数据
			$ADD = _getAdd($table,$data);
			if ($ADD && $name) {
				session($name,null);								
			}
			return $ADD;
		//}	
    }
	
    /**
     * 数据修改
     */
    function _dataEdit($table,$where,$data2,$data,$msg,$name,$cache=''){
		
		//$type = $this->_emptyChecked($data,$msg);
		
		//一维数组
		//if ($type == '1') {
			
			//添加数据
			$ADD = _getEdit($table,$where,$data2,$data,$cache);
			if ($ADD && $name) {
				session($name,null);								
			}
			return $ADD;
		//}	
    }
	
	function StoreClass() {
		unset($cache);
		$cache = 'gc_class_data';
		//S('gc_class_data',null);
		if (S($cache)) {
			$store_class = S($cache) ;
		} else {
			unset($str);
			unset($data);			
			$str = "gc_parent_id=%d";
			$data = array(0) ;							
			$store_class = _getList('goods_class',$str,$data);
			foreach ($store_class as $key=>$v) {			
				unset($son_1);
				$son_1 = goods_class_son('gc_parent_id',$v['gc_id']) ;
				foreach ($son_1 as $k=>$vv) {
					$son_1 [$k]['son_2'] = goods_class_son('gc_parent_id',$vv['gc_id']) ;
				}
				$store_class [$key] ['son_1'] = $son_1 ;				
			}
			S($cache,$store_class);	
		}
		return $store_class ;
	}
	
	/*
	*经营类目查询
	*
	*/
	function ChackClass($id) {
		//会员是否是自营店铺
		 
		//if (!$_SESSION['user_id']) {
			$getgrade_1 = _getOne('goods_class','gc_id=%d',array($id)) ;
			$getgrade_1 && $gc_1 = $getgrade_1 ['gc_parent_id'] ;
			$gc_1 && $getgrade_2 = _getOne('goods_class','gc_id=%d',array($gc_1)) ;
			$getgrade_2 && $gc_2 = $getgrade_2 ['gc_parent_id'] ;
			$gc_2 && $getgrade_3 = _getOne('goods_class','gc_id=%d',array($gc_2)) ;
			if ($gc_2) {
				 $result ["gc_1"] = $gc_2 ; //第1级分类
				 $result ["gc_name_1"] = $getgrade_3 ['gc_name'] ;
				 $result ["gc_2"] = $gc_1 ; //第2级分类
				 $result ["gc_name_2"] = $getgrade_2 ['gc_name'] ;
				 $result ["gc_3"] = $id ; //第3级分类
				 $result ["gc_name_3"] = $getgrade_1 ['gc_name'] ;
			 } else if(!$gc_2 && $gc_1) {
				 $result ["gc_1"] = $gc_1 ; //第1级分类
				 $result ["gc_name_1"] = $getgrade_2 ['gc_name'] ;
				 $result ["gc_2"] = $id ; //第2级分类
				 $result ["gc_name_2"] = $getgrade_1 ['gc_name'] ;
				 $result ["gc_3"] = 0 ; //第3级分类
				 $result ["gc_name_3"] =	0 ;		 
			} else if (!$gc_1) {
				 $result ["gc_1"] = $id ; //第1级分类
				 $result ["gc_name_1"] =	$getgrade_1 ['gc_name'] ;
				 $result ["gc_2"] = 0 ; //第2级分类
				 $result ["gc_name_2"] =	0 ;
				 $result ["gc_3"] = 0 ; //第3级分类	
				 $result ["gc_name_3"] =	0 ;
			}			
		//} 
		return $result ;		
	}
	
	/*
	*店铺获取商家文件夹的图片
	*
	*/
	function read_all_dir($dir,$cache=''){
		
        $result = array();
		
		if (S($cache) && $cache) {
			$result = S($cache);
		}  else {
			
			$handle = opendir($dir);
			if ($handle) {
				while (($file = readdir($handle)) !== false) {
					
					if ($file != '.' && $file != '..') {
						$cur_path = $dir. DIRECTORY_SEPARATOR .$file;
						if (is_dir($cur_path)) {
							
							$result[$_SESSION['user_id'].DIRECTORY_SEPARATOR .$file] = $this->read_all_dir($cur_path);
						} else {
							$dir_str = explode('/',$dir) ;
							$urlstr = '';
							for ($i=0;$i<=count($dir_str);$i++) {
								if ($i>0 && $i<count($dir_str)) {
									$urlstr = $urlstr.'/'.$dir_str[$i];
								}
							}						
							$cur_path = $urlstr.DIRECTORY_SEPARATOR .$file ;
							
							$result[] = $cur_path;
						}
					}
				}
				closedir($handle);
			}
			//缓存
			if ($cache) {
				S($cache,$result);
			}					
		}

        return $result;
    }

		
}
